package cn.wjh.business.service.impl;

import cn.wjh.business.domain.Comment;
import cn.wjh.business.domain.Focus;
import cn.wjh.business.domain.Works;
import cn.wjh.business.domain.vo.MailVo;
import cn.wjh.business.service.*;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.domain.SysUserPost;
import com.ruoyi.system.mapper.SysUserPostMapper;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.impl.SysPostServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class MyCenterService implements IMyCenterService {
	@Resource
	private IMailService mailService;

	@Resource
	private ISysUserService userService;


	@Resource
	private SysPermissionService permissionService;

	@Resource
	private SysUserPostMapper sysUserPostMapper;

	@Resource
	private SysPostServiceImpl sysPostService;

	@Resource
	private ICollectService collectService;

	@Resource
	private IWorksService worksService;

	@Resource
	private IFocusService focusService;

	@Resource
	private ICommentService commentService;

	@Override
	public boolean bindMail(MailVo mailVo) {
		if (!mailService.verifyCode(mailVo)) {
			throw new ServiceException("验证码或邮箱错误!");
		}
		Long userId = SecurityUtils.getUserId();
		SysUser user = new SysUser();
		user.setUserId(userId);
		user.setEmail(mailVo.getEmail());
		/*绑定邮箱*/
		return userService.updateById(user);

	}

	/**
	 * 获取用户信息
	 */
	@Override
	public AjaxResult getInfo() {
		SysUser user = SecurityUtils.getLoginUser().getUser();//当前登录用户
		/*角色集合*/
		Set<String> roles = permissionService.getRolePermission(user);
		/*权限集合*/
		Set<String> permissions = permissionService.getMenuPermission(user);
		/*身份集合*/
		Set<String> posts = getPostsSet(user.getUserId());
		List<Long> postIds = getPostIds(user.getUserId());
		/*收藏量*/
		Long collectTotal = collectService.countByCurrentUser(user);
		/*作品数量*/
		Works works1 = new Works();
		works1.setUserId(user.getUserId());
		List<Works> works = worksService.myWorks(works1);
		/*粉丝数量*/
		Focus focus = new Focus();
		focus.setUserId(user.getUserId());
		List<Focus> fans = focusService.myFans(focus);
		/*评论数量*/
		Comment comment = new Comment();
		comment.setUserId(user.getUserId());
		List<Comment> comments = commentService.myComment(comment);
		/*未读已读的消息数量*/
		Comment comment1 = new Comment();
		comment1.setReceiveId(user.getUserId());
		Map<String, Long> readAndNotReadCount = commentService.readAndNotReadCount(comment1);


		HashMap<String, Long> countInfo = new HashMap<>();
		countInfo.put("collect", collectTotal);
		countInfo.put("works", (long) works.size());
		countInfo.put("fans", (long) fans.size());
		countInfo.put("comment", (long) comments.size());
		AjaxResult ajax = AjaxResult.success();
		ajax.put("user", user);//用户信息
		ajax.put("roles", roles);//用户角色
		ajax.put("permissions", permissions);//用户权限
		ajax.put("posts", posts);//用户认证身份
		ajax.put("postIds", postIds);//用户认证身份id
		ajax.put("countInfo", countInfo);//用户数量信息
		ajax.put("readAndNotReadCount", readAndNotReadCount);//未读已读的消息数量
		return ajax;
	}

	/**
	 * 获取身份集合
	 *
	 * @param userId
	 * @return
	 */
	@Override
	public Set<String> getPostsSet(Long userId) {
		HashSet<String> posts = new HashSet<>();
		List<SysUserPost> sysUserPostList = sysUserPostMapper.getAllByUserId(userId);
		List<SysPost> postList = sysUserPostList.stream().map(up -> sysPostService.selectPostById(up.getPostId())).collect(Collectors.toList());
		for (SysPost post : postList) {
			posts.add(post.getPostName());
		}
		return posts;
	}

	/**
	 * 获取身份Id
	 *
	 * @param userId
	 * @return
	 */
	@Override
	public List<Long> getPostIds(Long userId) {
		List<SysUserPost> sysUserPostList = sysUserPostMapper.getAllByUserId(userId);
		List<SysPost> posts = sysUserPostList.stream().map(up -> sysPostService.selectPostById(up.getPostId())).collect(Collectors.toList());
		return posts.stream().map(SysPost::getPostId).collect(Collectors.toList());
	}
}
